package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Produto;

public class ProdutoDAO {
	
	
	public boolean insert(Produto p){
		
		String sql = "insert into produto values (?, ?, ?, ?, ?)";
		
		
		try{
			
			  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, p.getStrCodigo());
			   prep.setString(2,p.getStrDescr());
			   prep.setDouble(3, p.getDlValor());
			   prep.setDouble(4, p.getDlPeso());
			   prep.setString(5, p.getFornecedor().getCNPJ());
			   
			   prep.execute();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return true;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return false;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return false;
			
		}
		

	
	}
	
	/*
	public boolean update(Produto p){
		
		
		String sql = "update produto set codigo = ?, descricao = ?, valor = ?, peso = ? where codigo = ?";
		
		
		try{
			
			  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, p.getStrCodigo());
			   prep.setString(2,p.getStrDescr());
			   prep.setDouble(3, p.getDlValor());
			   prep.setDouble(4, p.getDlPeso());
			   prep.setString (5, p.getStrCodigo());
			   
			   prep.execute();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return true;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return false;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return false;
			
		}	
	
		
	}
	
	*/
	/*
	public boolean delete(Produto p){
		
		
		String sql = "delete from produto where codigo = ?";
		
		
		try{
			
			  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, p.getStrCodigo());
			 
			   prep.execute();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return true;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return false;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return false;
			
		}	

	}
*/
	public static ResultSet selectALL(Produto p){
		
		
		String sql = "select * from produto where codigo = ?";	
		
		try{
			
		  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			   prep.setString (1, p.getStrCodigo());
			 
			   ResultSet rs = prep.executeQuery();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return rs;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return null;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return null;
			
		}	

	}
	
	
	
	public static ResultSet selectALL(){
		
		
		String sql = "select p.codigo, p.descricao, p.valor, p.peso, f.razao_social from produto as p inner join fornecedor as f on p.cnpj_fornecedor = f.cnpj";	
		
		try{
			
		  	
			   PreparedStatement prep = ConnectionFactory.getInstance().prepareStatement(sql);
			 
			   ResultSet rs = prep.executeQuery();
			   
			   ConnectionFactory.getInstance().close();
			   
			   return rs;
			   
	
		}catch(SQLException e){
			
			e.getMessage();
			return null;
			
		}catch(Exception ex){
			
			ex.getMessage();
			return null;
			
		}	

	}
		
	
}
